package cn.hyy.hrm.controller;


import cn.hyy.basic.util.AjaxResult;
import cn.hyy.hrm.controller.vo.EmployeeVo;
import cn.hyy.hrm.Employee;
import cn.hyy.hrm.service.IEmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class LoginController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 登录
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody Employee employee){

        Employee loginUser = employeeService.getOne(new QueryWrapper<Employee>().eq("username", employee.getUsername()));

        if(loginUser==null){
            return AjaxResult.me().setSuccess(false).setMessage("用户名不存在!");
        }
        if(!(loginUser.getPassword().equals(employee.getPassword()))){
            return AjaxResult.me().setSuccess(false).setMessage("密码错误!");
        }

        //map一时爽，维护火葬场，建议编写vo实体类，响应给前端的数据的封装的对象
        EmployeeVo employeeVo = new EmployeeVo();
        //对象属性的拷贝，Spring的工具类
        BeanUtils.copyProperties(loginUser,employeeVo);
        return AjaxResult.me().setSuccess(true).setMessage("登录成功！").setResultObj(employeeVo);
    }

}
